#include "widget.h"
#include "ui_widget.h"
#include <QSystemTrayIcon>
#include <QDebug>

#define MAIN_WIDGET_WIDTH   300
#define MAIN_WIDGET_HEIGHT  200
#define FIX_SIZE 4

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    m_dockId = getDockWid();
    connect(QApplication::desktop(), &QDesktopWidget::resized, this, [=]() {
        qDebug()<<"#####QDesktopWidget::resized";
        updataWidgetPos();
    });
    connect(QApplication::desktop(), &QDesktopWidget::workAreaResized, this, [=](){
        qDebug()<<"#####QDesktopWidget::workAreaResized";
        updataWidgetPos();
    });
    connect(QApplication::primaryScreen(),&QScreen::virtualGeometryChanged,this, [=](){
        qDebug()<<"#####QDesktopWidget::virtualGeometryChanged";
        updataWidgetPos();
    });
    connect(QApplication::desktop(), &QDesktopWidget::screenCountChanged, this, [=]() {
        qDebug()<<"#####QDesktopWidget::screenCountChanged";
        updataWidgetPos();
    });
    connect(QApplication::desktop(), &QDesktopWidget::primaryScreenChanged, this, [=]() {
        qDebug()<<"#####QDesktopWidget::primaryScreenChanged";
        updataWidgetPos();
    });
    connect(QApplication::primaryScreen(),&QScreen::geometryChanged,this, [=]() {
        qDebug()<<"#####QScreen::geometryChanged";
        updataWidgetPos();
    });


    this->setWindowFlags(Qt::FramelessWindowHint | windowFlags() ); //窗口无边框
    this->setFixedSize(MAIN_WIDGET_WIDTH, MAIN_WIDGET_HEIGHT); //应用设置窗口大小，showMainWidget()根据窗口大小计算显示位置

    m_trayicon = new QSystemTrayIcon(this);
    m_trayicon->setIcon(QIcon::fromTheme("ukui-menu")); //设置托盘图标
    m_trayicon->setToolTip(QString::fromLocal8Bit("tray demo")); //设置托盘toolip
    connect(m_trayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
            this, SLOT(ActiveTray(QSystemTrayIcon::ActivationReason))); //绑定托盘点击信号
    m_trayicon->setVisible(true); //设置托盘可见

}


Widget::~Widget()
{
    delete ui;
}

/* 显示主窗口  */
void Widget::showMainWidget()
{
    updataWidgetPos();
    if(this->isVisible())
        this->hide();
    else
        this->show();
}

void Widget::updataWidgetPos()
{
    QRect clickPoint = m_trayicon->geometry(); //鼠标点击位置坐标
    QList<int> list = getWorkAreaGeometry(); //获取主屏可用区域
    int panelPos = getPanelPosition(); //获取任务栏位置

    int showX,showY;
    if(panelPos == PanelPos::top) {
        qDebug()<<" top ";
        if(clickPoint.x()+this->width() > list.at(0)+list.at(2)) {
            showX =   list.at(0)+list.at(2)- this->width() - FIX_SIZE;
        } else {
            showX = clickPoint.x() - FIX_SIZE;
        }
        showY = list.at(1) + FIX_SIZE;
    } else if(panelPos == PanelPos::left) {
        if(clickPoint.y() + this->height() > list.at(1)+list.at(3)) {
            showY =   list.at(1)+list.at(3)- this->height() - FIX_SIZE;
        } else {
            showY = clickPoint.y();
        }
        showX = list.at(0) + FIX_SIZE;
    } else if(panelPos == PanelPos::right) {
        if(clickPoint.y() + this->height() > list.at(1)+list.at(3)) {
            showY =   list.at(1)+list.at(3)- this->height() - FIX_SIZE;
        } else {
            showY = clickPoint.y();
        }
        showX = list.at(0) + list.at(2) - this->width() - FIX_SIZE;
    } else if(panelPos == PanelPos::bottom) {
        if(clickPoint.x()+MAIN_WIDGET_WIDTH > list.at(0)+list.at(2)) {
            showX =   list.at(0)+list.at(2)-this->width() - FIX_SIZE;
        } else {
            showX = clickPoint.x() - FIX_SIZE;
        }
        showY = list.at(1) + list.at(3) -this->height() - FIX_SIZE;
    }
    this->move(showX,showY);
}

void Widget::ActiveTray(QSystemTrayIcon::ActivationReason reason)
{
    switch(reason) {
        case QSystemTrayIcon::Context:
            qDebug()<<">>>>>>> 鼠标右键点击~";
            break;
        case QSystemTrayIcon::DoubleClick:
            break;
        case QSystemTrayIcon::Trigger:
            qDebug()<<">>>>>>> 鼠标左键点击~";
            showMainWidget();
            break;
        default:
        break;
    }
}

WId Widget::getDockWid()
{
    WId dockid;
    QList<WId> windows = KWindowSystem::windows();
    for (auto it = windows.cbegin(), end = windows.cend(); it != end; ++it) {
        KWindowInfo kwinfo(*it, NET::WMWindowType);
        if (kwinfo.windowType(NET::DockMask) == NET::Dock) {
            dockid = *it;
            qDebug()<<"#####"<<dockid;
        };
    }
    return dockid;
}

/* 获取可用区域 */
QList<int> Widget::getWorkAreaGeometry()
{
    KWindowInfo info(m_dockId, 0, NET::WM2ExtendedStrut);

    QList<int> screensize;
    int screenX;
    int screenY;
    int screenWidth;
    int screenHeight;

    getPanelPosition();

    switch(m_panelPosition) {
    case 0:
        screenX = 0;
        screenY = 0;
        screenWidth = info.extendedStrut().bottom_end + 1;
        screenHeight = QApplication::primaryScreen()->size().height() - info.extendedStrut().bottom_width;
        screensize<<screenX<<screenY<<screenWidth<<screenHeight;
        qDebug()<<screensize;
        break;
    case 1:
        screenX = 0;
        screenY = info.extendedStrut().top_width;
        screenWidth = info.extendedStrut().top_end + 1;
        screenHeight = QApplication::primaryScreen()->size().height() - info.extendedStrut().top_width;
        screensize<<screenX<<screenY<<screenWidth<<screenHeight;
        qDebug()<<screensize;
        break;
    case 2:
        screenX = info.extendedStrut().left_width;
        screenY = 0;
        screenWidth = QApplication::primaryScreen()->size().width() - info.extendedStrut().left_width;
        screenHeight = info.extendedStrut().left_end + 1;
        screensize<<screenX<<screenY<<screenWidth<<screenHeight;
        qDebug()<<screensize;
        break;
    case 3:
        screenX = 0;QApplication::desktop()->width() - info.extendedStrut().right_width;
        screenY = 0;
        screenWidth = QApplication::primaryScreen()->size().width() - info.extendedStrut().right_width;
        screenHeight = info.extendedStrut().right_end + 1;
        screensize<<screenX<<screenY<<screenWidth<<screenHeight;
        qDebug()<<screensize;
        break;
    }
    return screensize;
}

/* 获取任务栏位置 */
int Widget::getPanelPosition()
{
    KWindowInfo info(m_dockId, 0, NET::WM2ExtendedStrut);
    if (info.extendedStrut().bottom_width != 0) {
        m_panelPosition = 0;
    }
    if (info.extendedStrut().top_width != 0) {
        m_panelPosition = 1;
    }
    if (info.extendedStrut().left_width != 0) {
        m_panelPosition = 2;
    }
    if (info.extendedStrut().right_width != 0) {
        m_panelPosition = 3;
    }
    return m_panelPosition;
}
